package com.Controls;

import com.HMI.Main;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;

public class pHControl {
	// Controller parameters (for tuning)
	//private final double dblRef = 7.8; // Reference pH value
	//private final double dblPGain = 0.8; // P control gain

	//private final double dblPHStrk = 0.05; // Added pH per "stroke"
	private static final int intTStrk = 100; // Signal time in ms for 1 stroke

	private static GpioController gpio = GpioFactory.getInstance();
	private static GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(
			RaspiPin.GPIO_01, PinState.LOW);
	
	public static void Control() throws InterruptedException {
		//double dblSensor; // Sensor reading in pH
		// double dblPHExp; // Expected pH rise from injection

		//dblSensor = dblPHSensor;
		Solenoid();
	}

	private double Controller(double dblSensor) throws InterruptedException {
		/*
		double dblError; // Difference between ref and sensor
		double dblSignal; // Input to plant

		dblError = dblSensor - dblRef;
		
		if (dblError < -0.5) {
			return -1.0; // Ignore the case of injection Cl to balance pH for
							// now
		} else
		
		if (dblError < 0)
			dblError = 0; // Assume no/minimal overshoot

		dblSignal = dblPGain * dblError / dblPHStrk; // Apply PGain% of strokes
														// required to equal the
														// error
		
		Solonoid(dblSignal);

		return (dblPGain * dblError);
		*/
		return 0.0;
	}

	private static void Solenoid() throws InterruptedException {

		System.out.println("Solenoid High");
		pin.high();
		Thread.sleep(intTStrk);
		System.out.println("Solenoid Low");
		pin.low();
	}
}
